'use strict';
// const { values } = require('sequelize/types/lib/operators');
const { createBlogFailInfo } = require('../utils/ErrorModel');
const { ErrorModel, SuccessModel } = require('../utils/resultModel');

const PAGE_SIZE = 5;
module.exports = app => {
  class BlogController extends app.Controller {
    // 创建博客
    async createBlog() {
      const { ctx } = this;
      const { content, image } = ctx.request.body;
      // xss防范
      // content = ctx.helper.escape(content);
      const { id: userId } = ctx.session.userInfo;

      const REG_AT_WHO = /@(.+?)\s-\s(.+?)\b/g
      const atUserNameList = [];
      content.replace(REG_AT_WHO, (matchStr, nickName, userName) => {
        atUserNameList.push(userName);
        return matchStr;
      });

      // promise.all() => []
      // 获取用户列表
      const atUserList = await Promise.all(atUserNameList.map(async name => {
        const result = await this.service.user.getUserInfo(name)
        return result;
      }))
      // console.log(atUserList)
      // 获取用户id列表
      const atUserIdList = atUserList.map(user => user.id)

      try {
        const result = await this.service.blog.create({ content, userId, image });
        // 向atRelation表中写数据
        atUserIdList.map(async userId => {
          await this.service.atRelation.createAtRelation(result.id, userId)
        })
        ctx.body = new SuccessModel(result);
      } catch (err) {
        console.log(err);
        ctx.body = new ErrorModel(createBlogFailInfo);
      }
    }

    // 加载更多
    async loadProfileMore() {
      // 获取参数
      let { userName, pageIndex } = this.ctx.params; // {userName:"小明",pageIndex:2}
      pageIndex = parseInt(pageIndex);
      // 定义变量userId 值是用户的id
      // const { userName } = this.ctx.params;
      const pageSize = PAGE_SIZE;
      // 获取博客信息 调用service 中的方法
      const result = await this.service.blog.getBlogList(
        { userName, pageIndex, pageSize });
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize,
      });
    }
    // 广场加载更多
    async loadSquareMore() {
      // 获取参数
      let { pageIndex } = this.ctx.params; // {userName:"小明",pageIndex:2}
      pageIndex = parseInt(pageIndex);
      const pageSize = 10;
      // 获取博客信息 调用service 中的方法
      const result = await this.service.blog.getBlogList(
        { pageIndex, pageSize });
      // 响应数据
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize,
      });
    }
    // 首页的加载更多
    async loadHomeBlogMore() {
      // 在session中获取用户信息
      const { id } = this.ctx.session.userInfo;
      // 获取pageIndex
      let { pageIndex } = this.ctx.params;
      pageIndex = parseInt(pageIndex);
      // 获取关注人博客
      const result = await this.service.blog.getFollowerBlogList({
        userId: id,
        pageIndex,
      });
      // 响应数据
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize: 10,
      });
    }
    // AtMe的加载更多
    async loadAtMeBlogMore() {
      // 获取userId
      const { id: userId } = this.ctx.session.userInfo;
      // 获取pageIndex
      const { pageIndex } = this.ctx.params;
      // getAtMeBlogList({userId,pageIndex,PageSize})
      const result =await this.service.atRelation.getAtMeBlogList({ userId, pageIndex });

      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize: 10,
      });
    }
  }
  return BlogController;
};
